<?php

/**
 * Description of MethodFunctions
 *
 * @author a.novikov
 */
class MethodFunctions {

	/**
	 *
	 * @param object|string $class
	 * @param string $methodName
	 * @param mixed $arguments
	 * @return mixed
	 */
	public static function call($class, $methodName, $arguments = array()) {
		ArrayFunctions::makeArray($arguments);
		$callback = array($class, $methodName);
		if (!is_callable($callback)) {
			$className = ObjectFunctions::getClassName($class);
			throw new Exception("Can't call method: '{$className}::{$methodName}()' not found");
		}

		return call_user_func_array($callback, $arguments);
	}

	/**
	 *
	 * @param object|string $class
	 * @param string $methodName
	 * @param mixed $arguments
	 * @param string $methodNamePrefix
	 * @param bool $ucfirst
	 * @return mixed
	 */
	public static function callPrefix($class, $methodName, $arguments = array(), $methodNamePrefix = '', $ucfirst = true, $veryfyType = true) {
		if ($ucfirst) {
			$methodName = StringFunctions::ucfirst($methodName);
		}
		$methodName = $methodNamePrefix.$methodName;
		if ($veryfyType) {
			ArrayFunctions::makeArray($arguments);
		}
		return self::call($class, $methodName, $arguments );
	}

	/**
	 *
	 * @param object|string $class
	 * @param string $varName
	 * @param mixed $value
	 */
	public static function callSetter($class, $varName, $value, $ucfirst = true) {
		return self::callPrefix($class, $varName, array($value), 'set', $ucfirst);
	}

	/**
	 *
	 * @param object|string $class
	 * @param string $underscoreVarName
	 * @param mixed $value
	 */
	public static function callSetterUnderscore($class, $underscoreVarName, $value) {
		$varName = StringFunctions::underscoreToCamelCase($underscoreVarName);
		return self::callSetter($class, $varName, $value, false);
	}

	/**
	 *
	 * @param object|string $class
	 * @param string $varName
	 * @return mixed
	 */
	public static function callGetter($class, $varName, $ucfirst = true) {
		return self::callPrefix($class, $varName, array(), 'get', $ucfirst);
	}

	/**
	 *
	 * @param object|string $class
	 * @param string $underscoreVarName
	 * @return mixed
	 */
	public static function callGetterUnderscore($class, $underscoreVarName) {
		$varName = StringFunctions::underscoreToCamelCase($underscoreVarName);
		return self::callGetter($class, $varName, false);
	}
}